﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace web.component
{
    public class Ufile
    { 
        #region FILE/ IMAGE
        //lay anh san pham
        public static string GetImage(string img)
        {
            try
            {
                if (img == "0" || string.IsNullOrEmpty(img))
                    return "/images/site/no_image.jpg";
                else
                    return img;
            }
            catch
            {
                return "/images/site/no_image.jpg";
            }
        }
         

        public static string LoadImage(string siteurl,  string img)
        {
            return string.Format("<img src='{0}{1}' style='width:100px;height:100px'/>",siteurl, GetImage(img));
        }
        /// <summary>
        /// Xóa ảnh
        /// </summary>
        /// <param name="location">Đường dẫn của ảnh</param>
        public static void DelFileWithLocation(string location)
        {
            try
            {
                System.IO.FileInfo FI = new System.IO.FileInfo(location);
                if (FI.Exists)
                {
                    FI.Delete();
                }
            }
            catch
            { }
        }

        public static string FileSize(string location)
        {
            System.IO.FileInfo FI = new System.IO.FileInfo(location);
            string size = "0";
            double _size;
            if (FI.Exists == true)
            {
                if (FI.Length < 1024) size = FI.Length.ToString() + "&nbsp;Byte";
                else
                {
                    _size = (FI.Length / 1024.0);
                    if (_size < 1024)
                    {
                        size = _size.ToString() + "&nbsp;Kb";
                    }
                    else
                    {
                        _size = _size / 1024.0;
                        size = _size.ToString() + "&nbsp; Mb";
                    }
                }
            }
            else
            {
                size = "0";
            }
            return size;
        }
        public static string FileName(string location)
        {
            System.IO.FileInfo FI = new System.IO.FileInfo(location);
            string name = ".jpg";
            if (FI.Exists == true)
            {
                name = FI.Name;
            }
            else
            {
                name = ".jpg";
            }
            return name;
        }
        public static string ExtFile(string fileName)
        {
            string rt = "";
            int countLenght = fileName.Length;
            try
            {
                rt = fileName.Substring((countLenght - 3), 3);
            }
            catch
            {
                rt = "";
            }
            return rt;
        }
        #endregion

        public static int ReadUserVisited(string filename)
        {
            int Visited = 0;
            FileInfo info = new FileInfo(filename);
            if (info.Exists)
            {
                StreamReader re = File.OpenText(filename);


                string input = "";
                while ((input = re.ReadLine()) != null)
                {
                    Visited = int.Parse(input);
                }

                re.Close();

            }
            else throw new Exception("File khong ton tai");
            return Visited;
        }
        public static void SaveUserVisited(String fileName, int number)
        {
            TextWriter tw = new StreamWriter(fileName, false);

            // write a line of text to the file
            tw.WriteLine(number.ToString());
            // close the stream
            tw.Close();
        }
    }
}
